﻿using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;

namespace Coleotrack.Core.Helper
{
    public static class StringHelper
    {
        /// <summary>
        /// Wandelt einen Zeichekette in einen MD5-Hashwert um.
        /// </summary>
        /// <param name="s">Umzuwandelnde Zeichenkette.</param>
        /// <returns></returns>
        public static string Md5(this string s)
        {
            if (s == null)
            {
                s = string.Empty;
            }

            var x = new MD5CryptoServiceProvider();
            byte[] bs = Encoding.UTF8.GetBytes(s);
            bs = x.ComputeHash(bs);

            var sb = new StringBuilder();
            foreach (byte b in bs)
            {
                sb.Append(b.ToString("x2").ToLower());
            }

            return sb.ToString();
        }

        /// <summary>
        /// Prüft, ob eine Zeichenkette eine gültige Emailadresse ist
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsValidEmailAddress(this string s)
        {
            if (s == null)
            {
                s = string.Empty;
            }
            var regex = new Regex(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$");
            return regex.IsMatch(s);
        }

        /// <summary>
        /// Prüft, ob eine Zeichenkette aus mindestens einem Zeichen existiert, das kein Leerzeichen ist
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static bool IsNotEmptyText(this string s)
        {
            return (s ?? string.Empty).Trim().Length > 0;
        }
    }
}
